package cn.com.dstz.admin.action;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

import cn.com.dstz.security.encoder.Md5PwdEncoder;
import cn.com.dstz.utils.DataBaseUtils;
import cn.com.dstz.utils.ValidUtils;
import cn.com.dstz.web.constants.Constants;
import cn.com.dstz.base.action.BaseAction;

/**
 *  修改信息主要为： 
 *  		居间管理修改自己的信息
 *  		普通管理员修改自己的信息（即为当前居间添加的管理员），当为当前居间添加的管理员，只允许修改密码，如果是居间则修改自己的信息
 *  
 * @author 叶装装
 *
 */
public class BasicAct extends BaseAction{
	
	public String toprofile( ){
		
		Map< String, Object > adminData = this.getAdminData( );
		
		String forward = "profile";
		
		
		if( adminData != null && !adminData.isEmpty( ) ){
			forward = getForward( adminData );
			getRequest( ).setAttribute( "data", adminData );
		}else{
			alertMsg( null );
		}
		
		//setAdminData( );
		
		return forward;
	}
	
	public void profile( ){
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		Integer adminid =  ( Integer )admin.get( "id" );
		//Integer groupid = ( Integer ) admin.get( "groupid" );
		Integer aid = ( Integer )admin.get( "topaid" );
		
		int validUpdateType = 0;
		
		Map< String, Object > adminData = this.getAdminData( );
		
		String forward = getForward( adminData );
		
		if( "agent".equals( forward ) ){
			validUpdateType = 1;
		}
		
		// 要根据分组ID, 进行数据的更新
		int validUpdateProfile = validUpdateProfile( validUpdateType ); // 首先进行数据的验证
		
		if( validUpdateProfile > 0 ){
			this.ajaxResponse( validUpdateProfile );
			return ;
		}
		
		this.ajaxResponse( this.getUpdate( validUpdateType, adminid, aid ) > 0 ? 0 : -1 );
		
	}
	
	public void pwd( ){
		
		int validUpdatePwd = validUpdatePwd( );
		
		if( validUpdatePwd > 0 ){
			this.ajaxResponse( validUpdatePwd );
			return;
		}
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		int update = baseService.update( "dstzadmin", DataBaseUtils.getData( new String[]{
				"password"
		}, new Object[]{
				newPassword
		} ), DataBaseUtils.getData( new String[]{
				"id"
		}, new Object[]{
				admin.get( "id" )
		} ) );
		
		if( update > 0 ){
			admin.put( "password", newPassword );
		}
		
		this.ajaxResponse( update > 0 ? 0 : -1  );
		return;
	}
	
	private int validUpdateProfile( int profile ){
		if( profile == 0 ){
			if( ValidUtils.validLengthCanEmpty( name, 32 )){
				return 1;
			}
			
			return 0;
		}
		
		if( ValidUtils.validLength( name, 3, 32 )){
			return 1;
		}
		
		if( ValidUtils.validLength( address, 3, 100 )){
			return 2;
		}
		
		if( ValidUtils.validLength( contactuser, 2, 11 )){
			return 3;
		}
		
		if( ValidUtils.validLength( email, 1, 50 )){
			return 4;
		}
		
		// 这个是联系电话
		if( ValidUtils.validLength( tel, 1, 13 )){
			return 5;
		}
		
		if( ValidUtils.validLength( homepage, 3, 50 )){
			return 6;
		}
		
		if( ValidUtils.validLength( serviceQQ, 5, 15 )){
			return 7;
		}
		
		if( ValidUtils.validLength( technologyQQ, 5, 15 )){
			return 7;
		}
		
		if( ValidUtils.validLength( openAccountQQ, 5, 15 )){
			return 7;
		}
		
		if( ValidUtils.validLength( cooperationQQ, 5, 15 )){
			return 7;
		}
		
		if( ValidUtils.validLength( complaintsQQ, 5, 15 )){
			return 7;
		}
		
		if( StringUtils.isNotBlank( description ) && description.length( ) > 200 ){
			return 8;
		}
		
		if(! StringUtils.equals( name, oldname )){
			int dbValid = dbValid( );
			if( dbValid > 0 ){
				return 9;
			}
		}
		
		
		return 0;
	}
	
	private int validUpdatePwd( ){
		if( StringUtils.isBlank( password )){
			return 1;
		}
		
		if( StringUtils.isBlank( newPassword )){
			return 2;
		}
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		if(!StringUtils.equals( password, admin.get( "password" ).toString( ) )){
			return 3;
		}
		
		return 0;
	}
	
	private String getForward( Map< String, Object > adminData ){
		
		Object agentId = adminData.get( "id" );
		Object hasAgent = adminData.get( "hasagent" );
		Integer intAgent = hasAgent == null ? 0 : ( Integer ) hasAgent; 
		
		if( agentId != null && intAgent == 1  ){
			return "agent";
		}
		return "profile";
	}
	
	private int getUpdate( int validType, Integer adminid, Integer aid){
		// 只是修改基本信息
		if( validType == 0 ){
			
			return baseService.update( "dstzadmin", DataBaseUtils.getData( new String[]{
					"name","telphone","qqnum"
			}, new Object[]{
					name,telphone,qqnum
			} ), DataBaseUtils.getData( new String[]{
					"id"
			}, new Object[]{
					adminid
			} ));
			
		}else{
			
			int update = baseService.update( "dstzagent", DataBaseUtils.getData( new String[]{
					"name",
					"telephone",
					"sjphone",
					"address",
					"email",
					"khqq",
					"jsqq",
					"kaihqq",
					"hzqq",
					"tsqq",
					"info",
					"homepage",
					
			}, new Object[]{
					name,
					tel,
					contactuser,
					address,
					email,
					serviceQQ,
					technologyQQ,
					openAccountQQ,
					cooperationQQ,
					complaintsQQ,
					description,
					homepage
			} ), DataBaseUtils.getData( new String[]{
					"id"
			}, new Object[]{
					aid
			} ) );
			
			return update;
			
		}
		
	}
	
	/**
	 * @deprecated
	 * @param groupid
	 * @param adminid
	 * @param aid
	 * @return
	 */
	@Deprecated
	@SuppressWarnings( "unused" )
	private int getUpdate( Integer groupid, Integer adminid, Integer aid ){
		
		// 这个只是查看自己的信息
		if( groupid == 1 || groupid == 4 ){
			return baseService.update( "dstzadmin", DataBaseUtils.getData( new String[]{
					"name"
			}, new Object[]{
					name
			} ), DataBaseUtils.getData( new String[]{
					"id"
			}, new Object[]{
					adminid
			} ));
		}else{
			
			int update = baseService.update( "dstzagent", DataBaseUtils.getData( new String[]{
					"name",
					"telephone",
					"sjphone",
					"address",
					"email",
					"khqq",
					"jsqq",
					"kaihqq",
					"hzqq",
					"tsqq",
					"info",
					"homepage",
					
			}, new Object[]{
					name,
					tel,
					contactuser,
					address,
					email,
					serviceQQ,
					technologyQQ,
					openAccountQQ,
					cooperationQQ,
					complaintsQQ,
					description,
					homepage
			} ), DataBaseUtils.getData( new String[]{
					"id"
			}, new Object[]{
					aid
			} ) );
			
			return update;
			
		}
		
	}
	
	private int dbValid( ){
		
		return baseService.countBySQL( "select count(*) as a from dstzagent where name=:name", DataBaseUtils.getData( new String[]{
											"name"
											}, new Object[]{
													name
											} ));
										
		
	}
	
	/**
	 * 获取数据
	 * @return
	 */
	private Map< String, Object > getAdminData( ){
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		String sql = "select ag.*, g.name as groupname, a.name as realname, a.username, a.qqnum,a.telphone,a.status as adminstatus, a.hasagent from dstzgroup g, dstzadmin a left outer join dstzagent ag on a.topaid = ag.id where a.groupid = g.id and a.id=" + admin.get( "id" ) ;
		
		return baseService.getData( sql );
	}
	
	/**
	 * @deprecated
	 */
	@Deprecated
	@SuppressWarnings( "unused" )
	private void setAdminData( ){
		
		Map< String, Object > admin = this.getEntityInSession( Constants.ADMIN_IN_SESSION );
		
		Integer adminid =  ( Integer )admin.get( "id" );
		Integer groupid = ( Integer ) admin.get( "groupid" );
		
		Map< String, Object > data = null;
		
		// 这是查询自己的
		if( groupid == 1 || groupid == 4 ){
			String sql = "select a.*, g.name as groupname, ag.name as agentname from dstzgroup g, dstzadmin a left outer join dstzagent ag on a.aid = ag.id where a.groupid = g.id and a.id=" + adminid;
			data = baseService.getData( sql );
		
		}else{
			String sql = "select ag.*, g.name as groupname, a.username, a.topaid  from dstzgroup g, dstzadmin a, dstzagent ag where a.aid = ag.id and g.id = a.groupid and a.id = " + adminid ;
			data = baseService.getData( sql );
		}
		
		// 如果是 0, 就表示没有居间的管理员，只能修改自己的信息，并不能修改居间的信息
		// 如果是1，就表示有居间的管理员，可以修改居间信息
		
	
		if( data == null || data.isEmpty( ) ){
			alertMsg( null );
		}
		getRequest( ).setAttribute( "data", data );
		
	}
	
	
	private static final long serialVersionUID = 1L;
	
	private String address;
	private String contactuser;
	
	private String email;
	private String tel;
	private String homepage;
	private String serviceQQ;
	private String technologyQQ;
	private String openAccountQQ;
	private String cooperationQQ;
	private String complaintsQQ;
	private String description;
	private String qqnum;
	private String telphone;
	
	public String getQqnum() {
		return qqnum;
	}

	public void setQqnum(String qqnum) {
		this.qqnum = qqnum;
	}

	public String getTelphone() {
		return telphone;
	}

	public void setTelphone(String telphone) {
		this.telphone = telphone;
	}


	private String name;
	private String oldname;
	
	
	public String getName( ) {
		return name;
	}

	public void setName( String name ) {
		if( StringUtils.isNotBlank( name )){
			name = decode( name );
		}
		this.name = name;
	}
	

	public String getAddress( ) {
		return address;
	}

	public void setAddress( String address ) {
		if( StringUtils.isNotBlank( address )){
			address = decode ( address );
		}
		this.address = address;
	}

	public String getContactuser( ) {
		return contactuser;
	}

	public void setContactuser( String contactuser ) {
		if( StringUtils.isNotBlank( contactuser )){
			contactuser = decode ( contactuser );
		}
		this.contactuser = contactuser;
	}

	public String getEmail( ) {
		return email;
	}

	public void setEmail( String email ) {
		if( StringUtils.isNotBlank( email )){
			email = decode( email );
		}
		this.email = email;
	}

	public String getTel( ) {
		return tel;
	}

	public void setTel( String tel ) {
		this.tel = tel;
	}

	public String getHomepage( ) {
		return homepage;
	}

	public void setHomepage( String homepage ) {
		if( StringUtils.isNotBlank( homepage )){
			homepage = decode( homepage );
		}
		this.homepage = homepage;
	}

	public String getServiceQQ( ) {
		return serviceQQ;
	}

	public void setServiceQQ( String serviceQQ ) {
		this.serviceQQ = serviceQQ;
	}

	public String getTechnologyQQ( ) {
		return technologyQQ;
	}

	public void setTechnologyQQ( String technologyQQ ) {
		this.technologyQQ = technologyQQ;
	}

	public String getOpenAccountQQ( ) {
		return openAccountQQ;
	}

	public void setOpenAccountQQ( String openAccountQQ ) {
		this.openAccountQQ = openAccountQQ;
	}

	public String getCooperationQQ( ) {
		return cooperationQQ;
	}

	public void setCooperationQQ( String cooperationQQ ) {
		this.cooperationQQ = cooperationQQ;
	}

	public String getComplaintsQQ( ) {
		return complaintsQQ;
	}

	public void setComplaintsQQ( String complaintsQQ ) {
		this.complaintsQQ = complaintsQQ;
	}

	public String getDescription( ) {
		return description;
	}

	public void setDescription( String description ) {
		if( StringUtils.isNotBlank( description )){
			description = decode ( description );
		}
		this.description = description;
	}


	public String getOldname( ) {
		return oldname;
	}

	public void setOldname( String oldname ) {
		if( StringUtils.isNotBlank( oldname )){
			oldname = decode( oldname );
		}
		this.oldname = oldname;
	}


	private Md5PwdEncoder pwdEncoder;
	
	private String password;
	private String newPassword;
	
	public String getPassword( ) {
		return password;
	}

	public void setPassword( String password ) {
		if( StringUtils.isNotBlank( password )){
			password = pwdEncoder.md5( password );
		}
		this.password = password;
	}

	public String getNewPassword( ) {
		return newPassword;
	}

	public void setNewPassword( String newPassword ) {
		if( StringUtils.isNotBlank( newPassword )){
			newPassword = pwdEncoder.md5( newPassword );
		}
		this.newPassword = newPassword;
	}

	public Md5PwdEncoder getPwdEncoder( ) {
		return pwdEncoder;
	}

	public void setPwdEncoder( Md5PwdEncoder pwdEncoder ) {
		this.pwdEncoder = pwdEncoder;
	}
	
	
	
	

}
